//
//  QRCodeViewController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/10/4.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "QRCodeViewController.h"

@interface QRCodeViewController ()
@property (nonatomic,copy) NSString *url;
@property (nonatomic,copy) NSString *indexUrl;
@end

@implementation QRCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNaviWith:@"我的二维码"];
    [self setMainView];
}

- (void)setMainView
{
    UIImageView *codeIV = [UIImageView new];
    if (@available(iOS 11.0, *)) {
        codeIV.frame = CGRectMake(30, TopBarHeight_xs+24, SCREENWIDTH-60, 411);
    } else {
        // Fallback on earlier versions
        codeIV.frame = CGRectMake(30, TopBarHeight+24, SCREENWIDTH-60, 411);
    }
    codeIV.tag = 88;
    codeIV.backgroundColor = [UIColor clearColor];
    codeIV.contentMode = UIViewContentModeScaleAspectFill;
    [codeIV sd_setImageWithURL:[NSURL URLWithString:self.qrUrl]placeholderImage:[UIImage imageNamed:@"Placeholder400"]];
    [self.view addSubview:codeIV];
    
    UIButton *headBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    headBtn.frame = CGRectMake(SCREENWIDTH-80, codeIV.bottom+6, 55, 25);
    headBtn.layer.cornerRadius = 13;
    headBtn.layer.masksToBounds = YES;
    headBtn.backgroundColor = goldColor;
    headBtn.titleLabel.font = FontWithSize(12);
    [headBtn setTitleColor:UIColorFromHex(0x020200) forState:UIControlStateNormal];
    headBtn.tag = 50;
    [headBtn setAttributedTitle:[TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:@"修改"] forState:UIControlStateNormal];
    [headBtn addTarget:self action:@selector(editImage:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:headBtn];
    
    UIButton *uploadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    uploadBtn.frame = CGRectMake(2*leftPadding, codeIV.bottom+60, SCREENWIDTH-80, 50);
    uploadBtn.backgroundColor = goldColor;
    uploadBtn.layer.cornerRadius = 25;
    uploadBtn.layer.masksToBounds = YES;
    uploadBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:18];
    [uploadBtn setTitleColor:UIColorFromHex(0x444444) forState:UIControlStateNormal];
    [uploadBtn setTitle:@"保存二维码" forState:UIControlStateNormal];
    [uploadBtn addTarget:self action:@selector(uploadBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:uploadBtn];
}

- (void)editImage:(UIButton *)btn
{
    UIImageView *headIV = (UIImageView *)[self.view viewWithTag:88];
    __weak __typeof(self) weakSelf = self;
    
    [self takePhoto];
    self.photoBlock = ^(UIImage * _Nonnull image) {
        dispatch_async(dispatch_get_main_queue(), ^{
            headIV.image = image;
            weakSelf.url = [weakSelf uploadImageUrl:image and:btn.tag];
        });
    };
}

#pragma mark -- 上传图片
- (NSString *)uploadImageUrl:(UIImage *)image and:(NSInteger)tag
{
    UIImage *imageNew = [TTKUtil imageWithImageSimple:image scaledToSize:CGSizeMake(450, 450)];
    NSData *imageData = UIImageJPEGRepresentation(imageNew, 0.5);
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:imageData forKey:@"file"];
    __weak __typeof(self) weakSelf = self;
    [[HttpRequest currentHttpRequest]uploadWithParams:dict andImageData:imageData block:^(NSString *SamllPhoto) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.indexUrl = SamllPhoto;
        });
    }];
    return weakSelf.indexUrl;
}

- (void)uploadBtn:(UIButton *)btn
{
    if (self.indexUrl.length == 0) {
        [self ShowHUDWithMessage:@"暂无修改图片，无需上传"];
        return;
    }
    //sellerWechatQrImage
    NSLog(@"data=%@",self.url);
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    [data setValue:[NSNumber numberWithInteger:self.id] forKey:@"id"];
    [data setValue:self.indexUrl forKey:@"sellerWechatQrImage"];
    [HttpRequest formPostPath:EditMyInfo_API params:data resultBlock:^(id Data, MsgModel *msgModel) {
        [self ShowHUDWithMessage:@"修改成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}


@end
